
package components;
import info.clearthought.layout.TableLayout;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

import utils.IConnection;
import utils.Settings;

import commands.CommandChatMessage;

/**
 * panel for chat
 */
public class ChatPanel extends RPSPanel {

	private static final long serialVersionUID = 1L;

	private static final String CHAT_FONT_NAME  = "Arial Unicode MS";
	private static final int    CHAT_FONT_STYLE = Font.PLAIN; 
	private static final int    CHAT_FONT_SIZE  = 14;
	
	private JTextArea	messageOutput;
	private JTextField	messageInput;
	private JScrollPane	scrolledMessageOutput;
	private RPSButton	sendButton;

	private IConnection conn;

	private String usernameFrom = null;

	
	public ChatPanel (IConnection conn, int height) {
        this.conn = conn;

        this.usernameFrom = conn.getUsername();

		this.messageOutput = new JTextArea (height, height);
		this.messageOutput.setFont(new Font(CHAT_FONT_NAME, CHAT_FONT_STYLE, CHAT_FONT_SIZE));
		this.messageOutput.setEditable(false);
		this.messageOutput.setLineWrap(true);
		this.messageOutput.setWrapStyleWord (true);
		this.scrolledMessageOutput = new JScrollPane (this.messageOutput);
		scrolledMessageOutput.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		
		this.messageInput = new JTextField ();

		this.sendButton = new RPSButton ("Send");

		double pref = TableLayout.PREFERRED, fill = TableLayout.FILL;
		double [][] sizes = {{fill}, {fill, 2, pref}};
		setLayout (new TableLayout (sizes));
		sizes = new double [][] {{TableLayout.FILL, pref},{pref}};
		JPanel bottomPanel = new JPanel (new TableLayout (sizes));

		bottomPanel.add (this.messageInput, "0,0");
		bottomPanel.add (this.sendButton, "1,0");
		
		add (this.scrolledMessageOutput, "0,0");
		add (bottomPanel, "0,2");

		
		//Add title to the component
		setBorder("Chat");
		
		//Add listener to the send event
		this.sendButton.addActionListener (
			new ActionListener () {
				public void actionPerformed (ActionEvent e) {
					sendMessage ();
				}
			}
		);

		this.messageInput.addKeyListener(
			new KeyAdapter () {
				// respond to the enter key
			    public void keyReleased(KeyEvent keyevent) {
			    	if (keyevent.getKeyCode() == KeyEvent.VK_ENTER) {
			    		sendMessage ();
			    	}
			    	else
			    	{
			    		sendButton.setEnabled(!messageInput.getText().isEmpty());
			    	}
			    }
			}
		);
	}


	protected void sendMessage () {
		String messageText = this.messageInput.getText();

		if (this.conn != null && !messageText.equals("")) {
		    this.messageOutput.append (makeMessage(this.usernameFrom, messageText));

		    // Send message and blank the message input box.
			CommandChatMessage chatMessage = new CommandChatMessage (messageText);
			this.conn.send (chatMessage);
			resetMessage();
		}
	}

	public void receiveMessage (String messageFrom, String message) {
	    this.messageOutput.append (makeMessage(messageFrom, message));
	    this.messageOutput.setCaretPosition (this.messageOutput.getText().length());
	}
	
	private void resetMessage()
	{
		this.messageInput.setText("");
		sendButton.setEnabled(false);
	}
	
	private String makeMessage(String from, String message)
	{
		return from + ": " + message + Settings.NEW_LINE;
	}
}
